# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from tools.logger import Logger

logger = Logger(logger="TestWeb").getlog()
class BasePage(object):
    """
    BasePage封装所有页面的公有方法，例如url、driver、find_element
    """

    # 构造函数里面的参数就是类的所有参数
    def __init__(self, selenuime_driver, base_url):
        self.driver = selenuime_driver
        self.url = base_url

    # 定义一个私有方法，其他类不能调用该方法
    def _open(self, url):
        self.driver.get(url)
        self.driver.maximize_window()

    # 定义open()方法，调用_open()方法
    def open(self):
        self._open(self.url)

    # 重写find_element()方法
    def find_emelemt(self, loc):
        flag = True
        ret_list = []
        try:
            obj = self.driver.find_element_by_xpath(loc)
            ret_list.append(flag)
            ret_list.append(obj)
            return ret_list
        except:
            flag = False
            ret_list.append(flag)
            print(u"页面中未找到%s元素" % loc)
            return ret_list

    # 定义script()方法，用于执行JS脚本，比如上传文件
    def script(self, src):
        self.driver.excute_script(src)

    # 定义页面跳转方法，比如有的页面有frame嵌套
    def switch_frame(self, loc):
        return self.driver.switch_to_frame(loc)

